<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="物业名称" prop="pmid">
        <el-select v-model="queryParams.pmid" placeholder="请选择物业名称" @change="clearCarshomeList"
                   clearable>
          <el-option
            v-for="item in this.propertyList"
            :key="item.pmid"
            :label="item.propertyName"
            :value="item.pmid">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="车场名称" prop="chname">
        <el-select v-model="queryParams.chname" placeholder="请选择车场名称" @visible-change="getListCars" clearable>
          <el-option
            v-for="item in this.carshomeList"
            :key="item.chid"
            :label="item.chname"
            :value="item.chname">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="车场类别" prop="chtid">
        <el-select v-model="queryParams.chtid" placeholder="请选择" clearable>
          <el-option
            v-for="item in this.typeList"
            :key="item.chtid"
            :label="item.tname"
            :value="item.chtid">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="车场状态">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option label="启用" :value=1></el-option>
          <el-option label="禁用" :value=0></el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sys-cars-home:home:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sys-cars-home:home:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sys-cars-home:home:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sys-cars-home:home:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="homeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="车场ID" align="center" prop="chid"/>
      <el-table-column label="车场名称" align="center" prop="chname"/>
      <el-table-column label="车场类别" align="center" prop="carsHomeType.tname"/>
      <el-table-column label="车场地址" align="center" prop="addr"/>
      <el-table-column label="所属物业" align="center" prop="propertyManagement.propertyName"/>
      <el-table-column label="泊位总数" align="center" prop="allNumber"/>
      <el-table-column label="剩余泊位" align="center" prop="num"/>
      <el-table-column label="满车位是否开闸" align="center" prop="info" :formatter="showinfo"/>
      <el-table-column label="支付离场时间(min)" align="center" prop="goTime" width="200"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="showstatus"/>
      <!--      <el-table-column label="是否删除" align="center" prop="deleted" />-->
      <!--      <el-table-column label="唯一编码，前台使用" align="center" prop="code" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleYoumian(scope.row)"-->
<!--          >充电优免规则-->
<!--          </el-button>-->

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLook(scope.row)"
          >查看
          </el-button>


          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sys-cars-home:home:edit']"
          >修改
          </el-button>


          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sys-cars-home:home:remove']"
          >删除
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleFree(scope.row)"
          >已有订单金额免费
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDisposition(scope.row)"
          >配置
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listHome, getHome, delHome, addHome, updateHome} from "@/api/modules/sys-cars-home/home";
import {listType} from "@/api/modules/sys-cars-home/type/type";
import router from "@/router";
import {listProperty} from "@/api/modules/sys-property/property";

export default {
  name: "Home",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车场管理表格数据
      homeList: [],
      // 车场管理表格数据
      carshomeList: [],
      // 物业数据
      propertyList: [],
// 车场类型表格数据
      typeList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chname: null,
        chtid: null,
        addr: null,
        pmid: null,
        allNumber: null,
        num: null,
        info: null,
        goTime: null,
        status: null,
        free: null,
      },

      // 查询参数
      queryParams2: {
        pmid: null,
      },
      // 查询参数
      type: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        status: null,
        deleted: null,
        tname: null
      },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
    this.getListProperty();
    // this.getListCars();
    this.getType();

  },
  methods: {
    showinfo(row, column, cellValue, index,) {
      if (cellValue) {
        return "开闸";
      }
      return "不开闸";
    },
    showstatus(row, column, cellValue, index,) {
      if (cellValue) {
        return "启用";
      }
      return "禁用";
    },


    /** 查询车场类型列表 */
    getType() {
      this.loading = true;
      listType(this.type).then(response => {
        this.typeList = response.rows;
      });
    },


    /** 查询车场管理列表 */
    getList() {
      this.loading = true;
      listHome(this.queryParams).then(response => {
        this.homeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询车场管理列表 */
    getListCars() {
      listHome(this.queryParams2).then(response => {
        this.carshomeList = response.rows;
      });
    },

    /** 查询物业列表 */
    getListProperty() {
      this.loading = true;
      listProperty(this.queryParams).then(response => {
        this.propertyList = response.rows;
        this.loading = false;
      });
    },
    //清除车场
    clearCarshomeList() {
      this.queryParams2.pmid = this.queryParams.pmid
      this.queryParams.chname = null
      this.getListCars()
    },


    // 表单重置
    reset() {
      this.form = {
        chid: null,
        chname: null,
        chtid: null,
        addr: null,
        addrcode: null,
        pmid: null,
        allNumber: null,
        num: null,
        info: null,
        goTime: null,
        status: null,
        free: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        code: null,
        operate: null
      };
      this.queryParams.status = null
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.chname = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.chid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },


    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.operate = "新增"
      this.$router.push({path: 'carshomeedit', query: this.form});
    },



    /** 删除按钮操作 */
    handleDelete(row) {
      const chids = row.chid || this.ids;
      this.$modal.confirm('是否确认删除车场管理编号为"' + chids + '"的数据项？').then(function () {
        return delHome(chids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sys-cars-home/home/export', {
        ...this.queryParams
      }, `home_${new Date().getTime()}.xlsx`)
    },


    // /** 车场充电优免规则操作 */
    // handleYoumian(row) {
    //   this.reset();
    //   const chid = row.chid || this.ids
    //   getHome(chid).then(response => {
    //     this.form = response.data;
    //     this.$router.push({path: 'youmian', query: this.form});
    //   });
    // },


    /** 车场查看操作 */
    handleLook(row) {
      this.reset();
      const chid = row.chid || this.ids
      getHome(chid).then(response => {
        this.form = response.data;
        this.form.operate = "查看";
        this.$router.push({path: 'carshomeedit', query: this.form});
        // this.title = "车场编辑操作";
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const chid = row.chid || this.ids
      getHome(chid).then(response => {
        this.form = response.data;
        // this.open = true;
        // this.title = "修改车场管理";
        this.form.operate = "修改";
        this.$router.push({path: 'carshomeedit', query: this.form});
      });
    },


    /** 订单费用免费操作 */
    handleFree(row) {
      this.$confirm('确定对该车场已有订单费用免费操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },

    /** 配置按钮操作 */
    handleDisposition(row) {
      this.reset();
      const chid = row.chid || this.ids
      getHome(chid).then(response => {
        this.form = response.data;
        this.$router.push({path: 'carshomerule', query: {chid: chid, chname: this.form.chname}});
      });
    },


  }
};
</script>
